package code.oz.org.test

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import android.util.DisplayMetrics
import android.view.*
import kotlinx.android.synthetic.main.fragment_main.*


/**
 * A placeholder fragment containing a simple view.
 */
class MainActivityFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_main, container, false)
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setHasOptionsMenu(true)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        (activity as AppCompatActivity).setSupportActionBar(toolbar)

        val manager = activity?.windowManager
        val outMetrics = DisplayMetrics()
        manager?.defaultDisplay?.getMetrics(outMetrics)

        val height = outMetrics.heightPixels
        val width = outMetrics.widthPixels

        if (height > 1920) {

            var layoutParams = app_bar.layoutParams

            layoutParams.height = width * 3 / 4

            app_bar.layoutParams = layoutParams

        } else {

            var layoutParams = app_bar.layoutParams

            layoutParams.height = width * 5 / 8

            app_bar.layoutParams = layoutParams
        }
    }


    override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {

        super.onCreateOptionsMenu(menu, inflater)

        menu?.clear()

        inflater?.inflate(R.menu.menu_main_fragment, menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        return super.onOptionsItemSelected(item)
    }


    override fun onOptionsMenuClosed(menu: Menu?) {
        super.onOptionsMenuClosed(menu)
    }


}
